<script setup lang="ts">
import { useAddressStore } from '@/stores'
import type { AddressItem } from '@/types/address'
import { onMounted } from 'vue'
import { watch } from 'vue'
// import { useAddressStore, useMemberStore } from '@/stores'
import { ref } from 'vue'
//子调用父
const emit = defineEmits<{
  (event: 'close'): void
}>()

const goNewAddress = () => {
  uni.navigateTo({ url: '/pagesMember/address-form/address-form' })
}
const query = defineProps<{
  addressList: AddressItem[]
}>()
type addressItemNow = AddressItem & {
  isSelectedAddress?: Boolean
}
const addressListNow = ref<addressItemNow[]>(
  query.addressList?.map((item) => {
    if (item.isDefault) {
      return { ...item, isSelectedAddress: true }
    } else {
      return { ...item, isSelectedAddress: false }
    }
  })
)
//是否选中地址
const onChaneg = (item: addressItemNow) => {
  // 将所有选项的 selected 设置为 false
  addressListNow.value.forEach((option) => {
    if (item.id !== option.id) {
      option.isSelectedAddress = false
    }
  })
  // 将选中的选项的 selected 设置为 true
  item.isSelectedAddress = !item.isSelectedAddress
}
watch(
  () => query.addressList,
  (newPropObject) => {
    console.log(newPropObject)

    addressListNow.value = newPropObject?.map((item) => {
      if (item.isDefault) {
        return { ...item, isSelectedAddress: true }
      } else {
        return { ...item, isSelectedAddress: false }
      }
    })
  }
)
//确定回显
const address = ref<addressItemNow>()
const onOk = () => {
  address.value = addressListNow.value.find((option) => {
    return option.isSelectedAddress === true
  })
  const addressStore = useAddressStore()
  addressStore.changeSelectedAddress(address.value as AddressItem)
  emit('close')
}

defineExpose({
  address
})
</script>

<template>
  <view class="address-panel">
    <!-- 关闭按钮 -->
    <text class="close icon-close" @tap="emit('close')"></text>
    <!-- 标题 -->
    <view class="title">配送至</view>
    <!-- 内容 -->
    <view class="content">
      <view class="item" v-for="item in addressListNow" :key="item.id">
        <view class="user">{{ item.receiver }}{{ item.contact }}</view>
        <view class="address"> {{ item.fullLocation }}{{ item.address }}</view>
        <text
          :class="[item.isSelectedAddress ? 'icon icon-checked' : 'icon icon-ring']"
          @tap="onChaneg(item)"
        ></text>
      </view>
    </view>
    <view class="footer">
      <view class="button primary" @tap="goNewAddress"> 新建地址 </view>
      <view v-if="addressList?.length" class="button primary" @tap="onOk">确定</view>
    </view>
  </view>
</template>

<style lang="scss">
.address-panel {
  padding: 0 30rpx;
  border-radius: 10rpx 10rpx 0 0;
  position: relative;
  background-color: #fff;
}

.title {
  line-height: 1;
  padding: 40rpx 0;
  text-align: center;
  font-size: 32rpx;
  font-weight: normal;
  border-bottom: 1rpx solid #ddd;
  color: #444;
}

.close {
  position: absolute;
  right: 24rpx;
  top: 24rpx;
}

.content {
  min-height: 300rpx;
  max-height: 540rpx;
  overflow: auto;
  padding: 20rpx;
  .item {
    padding: 30rpx 50rpx 30rpx 60rpx;
    background-size: 40rpx;
    background-repeat: no-repeat;
    background-position: 0 center;
    background-image: url(https://pcapi-xiaotuxian-front-devtest.itheima.net/miniapp/images/locate.png);
    position: relative;
  }
  .icon {
    color: #999;
    font-size: 40rpx;
    transform: translateY(-50%);
    position: absolute;
    top: 50%;
    right: 0;
  }
  .icon-checked {
    color: #27ba9b;
  }
  .icon-ring {
    color: #444;
  }
  .user {
    font-size: 28rpx;
    color: #444;
    font-weight: 500;
  }
  .address {
    font-size: 26rpx;
    color: #666;
  }
}

.footer {
  display: flex;
  justify-content: space-between;
  padding: 20rpx 0 40rpx;
  font-size: 28rpx;
  color: #444;

  .button {
    flex: 1;
    height: 72rpx;
    text-align: center;
    line-height: 72rpx;
    margin: 0 20rpx;
    color: #fff;
    border-radius: 72rpx;
  }

  .primary {
    color: #fff;
    background-color: #27ba9b;
  }

  .secondary {
    background-color: #ffa868;
  }
}
</style>
